
export function time(e, i) {
  this.selectedindex = i
  if (i === this.timeId) {
    this.statuss = !this.statuss
  } else {
    this.statuss = false
  }
  this.timeId = i
  if (this.statuss === false) {
    if (e === 'day') {
      this.day()
    } else if (e === 'week') {
      this.week()
    } else if (e === 'month') {
      this.month()
    }
  } else {
    this.$emit('start-date', '')
    this.$emit('end-date', '')
  }
}
export function day() {
  // 获取当天
  const startday = new Date(new Date(new Date().toLocaleDateString()).getTime());
  const endday = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
  return { startDate: dateToFormattwo(startday) + ' 00:00:00', endDate: dateToFormattwo(endday) + ' 23:59:59' };
}
// 3.获取本周开始和结束日期
export function week(Fn) {
  // 按周日为一周的最后一天计算
  var dateStart = new Date(new Date(new Date().toLocaleDateString()).getTime());
  // 今天是这周的第几天
  var today = dateStart.getDay();
  // 上周日距离今天的天数（负数表示）
  var stepSunDay = -today + 1;
  // 如果今天是周日
  if (today === 0) {
    stepSunDay = -7;
  }
  // 周一距离今天的天数（负数表示）
  var stepMonday = 7 - today;
  var time = dateStart.getTime();
  // var times = dateEnd.getTime();
  var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
  var sunday = new Date(time + stepMonday * 24 * 3600 * 1000 + 24 * 60 * 60 * 1000 - 1);
  return {
    startDate: dateToFormattwo(monday), endDate: dateToFormattwo(sunday)
  };
}

// 4.获取本月日期
export function month(Fn) {
  // 获取当前月的第一天
  var dateStart = new Date(new Date(new Date().toLocaleDateString()).getTime());
  dateStart.setDate(1);
  // 获取当前月的最后一天
  var dateEnd = new Date();
  var currentMonth = dateEnd.getMonth();
  var nextMonth = ++currentMonth;
  var nextMonthFirstDay = new Date(dateEnd.getFullYear(), nextMonth, 1);
  var oneDay = 1000 * 60 * 60 * 24;
  var end = new Date(nextMonthFirstDay - oneDay + 24 * 60 * 60 * 1000 - 1);
  return { startDate: dateToFormattwo(dateStart), endDate: dateToFormattwo(end) };
}
function dateToFormattwo(originDate) {
  const dt = new Date(originDate);
  const y = dt.getFullYear();
  const m = (dt.getMonth() + 1 + "").padStart(2, "0");
  const d = (dt.getDate() + "").padStart(2, "0");
  return `${y}-${m}-${d}`;
}
// 5.日期变换的方法
export function transferDate(date) {
  // 年
  var year = date.getFullYear();
  // 月
  var month = date.getMonth() + 1;
  // 日
  var day = date.getDate();

  var getHours = date.getHours()

  var getMinutes = date.getMinutes()

  var getSeconds = date.getSeconds()

  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (getHours >= 0 && getHours <= 9) {
    getHours = "0" + getHours;
  }
  if (getMinutes >= 0 && getMinutes <= 9) {
    getMinutes = "0" + getMinutes;
  }
  if (getSeconds >= 0 && getSeconds <= 9) {
    getSeconds = "0" + getSeconds;
  }
  var dateString = year + '/' + month + '/' + day + ' ' + getHours + ':' + getMinutes + ':' + getSeconds;
  return dateString;
}
